#include "stdafx.h"

const float Dot::verts[] = {
	1, 1,
	1, -1,
	-1, -1,
	1, 1,
	-1, 1,
	-1, -1
};
const int Dot::size = 10;

Dot::Dot(int x, int y) :x(x), y(y)
{
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);

	GLuint VBO;

	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
	
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
	glEnableVertexAttribArray(0);

	glBindVertexArray(0);
}

Dot::~Dot()
{
}

bool Dot::render()
{
	//ProgramMgr::getInstance()->get("pure")->use();

	glm::mat4 model;
	glm::mat4 scale(1.0f);
	scale = glm::scale(scale, glm::vec3(size, size, 1.0f));
	glm::mat4 translate(1.0);
	translate = glm::translate(translate, glm::vec3(x, y, 0.0f));
	model = translate * scale;

	Program * prog = ProgramMgr::getInstance()->get("pure");
	prog->use();
	prog->uniformmat4("model", model);
	prog->uniform3f("color", color);

	glBindVertexArray(VAO);
	glDrawArrays(GL_TRIANGLES, 0, 6);

	return true;
}

bool Dot::moveto(int dx, int dy)
{
	x = dx;
	y = dy;

	return true;
}

bool Dot::collide(int x, int y)
{
	bool X = x >= this->x - size && x <= this->x + size;
	bool Y = y >= this->y - size && y <= this->y + size;

	return X && Y;
}

void Dot::setColor(const glm::vec3 & color)
{
	this->color = color;
}